<template>
	<view class="home">

		<!-- tab区 -->
		<view class="tabBox" v-show="!skeleton">
			<u-tabs :list="questionList" :current="qidNum" @click="clicktab"></u-tabs>
		</view>
		<!-- 骨架屏 -->
		<view class="skeleton" v-show="skeleton">
			<u-skeleton :loading="true" rows="5" :animate="true"></u-skeleton>
		</view>

		<!-- 问题区域 v-if="qidNum+1 == item.qid" -->
		<view class="questionBox" v-for="questionList in questionList" v-if="qidNum+1 == questionList.name"
			:key="questionList._id._value" v-show="!skeleton">
			<!-- tag部分Box -->
			<view class="tagBox">
				<!-- tag项目 -->
				<view class="tagItem" v-for="tagList in tagList" :key="tagList.tagid" v-if="tagList.tagContent"
					:style="{backgroundColor:`${tagList.tagColor}`}">
					{{tagList.tagContent}}
				</view>

			</view>
			<!-- 考试界面区 -->
			<view class="examBox" v-show="mode == 'exam' || mode == 'diyExam'">
				<!-- 作答情况 -->
				<view class="resItem">
					答题情况：{{resOPS}}/{{examQueNum}}
				</view>
				<!-- 倒计时 -->
				<!-- <view class="timeCont">
					倒计时：
					<view class="time">
						<u-count-down :time="examTimeCount" format="HH:mm:ss"></u-count-down>
					</view>
				</view> -->
				<!-- 成绩 -->
				<view class="resItem">
					成绩：{{examOpsRight}}/{{examQueNum * 2}}
				</view>
			</view>

			<!-- 问题部分 -->
			<!-- 问题名称 -->
			<view class="questionTitle">

				{{questionList.questionContent}}
			</view>

			<!-- 题目图片 -->
			<!-- <view class="questionImg" v-if="tagList.questionImgType">
					
				</view> -->

			<!-- 选项ROW -->
			<view class="questionOptionRow" v-for="optionBt in optionBt" :key="optionBt.oid">
				<!-- 选项名 -->
				<view class="optionName">
					{{optionBt.oName}}.
				</view>
				<!-- 选项内容 -->
				<view class="optionContent">
					{{optionBt.optionContent}}
				</view>
			</view>


			<!-- 选项按钮 -->

			<view class="optionButtonBox">

				<view class="optionButtonItem" v-for="optionBt in optionBt" :key="optionBt.oName"
					@click=" answerType == false? getOption(`${optionBt.oid}`,`${optionBt.oName}`):''"
					:style="{border:`6rpx solid ${optionBt.borderColor}`}">
					{{optionBt.oName}}
				</view>

			</view>

			<!-- 解析区域Box -->
			<view class="analysisBox" v-if="answerType">
				<!-- 显示回答是否正确 -->

				<!-- 正确答案 -->
				<view class="analysisRight border">
					答案: {{questionList.answer}}
				</view>

				<!-- 用户选择的选项 -->
				<view class="userOption border">
					您选择: {{opNow}}
				</view>

				<!-- 解析 -->
				<view class="analysisContent border">
					解析: {{questionList.analysisContent}}
				</view>
			</view>
		</view>




		<!-- 上下题按钮和收藏区域 -->
		<view class="buttonRow">
			<!-- 收藏 -->
			<view @click="myCollect">
				<block>
					<u-button type="primary"  class="collect" color="#FBC02D" shape="circle" plain="true" v-if="!collectType">
						收藏
					</u-button>
					<u-button type="primary"  class="collect" color="#FBC02D" shape="circle" plain="true" v-else>
						已收藏
					</u-button>
				</block>
				
			</view>

			<!-- 上一题 -->
			<view class="nextButton">
				<u-button type="primary" @click="nextClick(0)" shape="circle" plain="true" 
					:disabled="qidNum==0? true:false">
					上一题
				</u-button>

			</view>

			<!-- 下一题 -->
			<view class="nextButton">
				<u-button type="primary" @click="nextClick(1)" shape="circle"
					:disabled="qidNum==`${questionList.length-1}`? true:false">
					下一题
				</u-button>
			</view>
		</view>

		<!-- 修改和删除的图标 -->
		<view class="modifyBox" @click="swtichSheetStaic"
			v-show="(mode == 'nomal'||mode == '0' || mode == '1') &&(uniIDHasRole('wxadmin')||uniIDHasRole('entryClerk'))"
			:style="{backgroundImage:`url(${sheetPic})`}">
			<u-action-sheet :actions="sheetList" cancelText="取消" :closeOnClickOverlay="true" :closeOnClickAction="true"
				@close="closeSheet" @select="selectSheet" :show="sheetStaic" round="16"></u-action-sheet>
		</view>

		<!-- 刷新图标 -->
		<view class="refresh" @click="clickRefresh" v-show="randomRefresh"
			:style="{backgroundImage:`url(${refreshPic})`}">
		</view>

		<!-- <view class="md">
			<u-parse :content="article" @preview="preview" @navigate="navigate" ></u-parse>
		</view> -->
		<!-- <u-parse :content="article" @navigate="navigate"></u-parse> -->

	</view>
</template>

<script>
	// import { marked } from 'marked'
	// import uParse from '@/components/u-parse/u-parse.vue'

	import {
		data
	} from '../../uni_modules/uview-ui/libs/mixin/mixin';

	// import uParse from "@/components/feng-parse/parse.vue"
	const db = uniCloud.database();
	const questionFun = uniCloud.importObject("questionFun", {
		customUI: true // 取消自动展示的交互提示界面
	}); //导入数据库云对象
	const dbCmd = db.command
	export default {
		// components: {
		//     uParse
		//   },
		data() {
			return {
				collectType:false,//题该题目是否收藏
				examOpsRight: 0, //考试模式得分
				//examTimeCount:150 * 60 * 60 * 1000,//考试模式倒计时
				examQueNum: 0, //考试模式有多少题
				resOPS: 0, //考试模式时作答了几题
				mode: "", //当前刷题模式
				oldTime: '', //页面打开时记录时间，防止恶意刷新用
				randomRefresh: false, //用于显示随机刷题刷新
				skeleton: true, //数据加载完毕后取消骨架屏
				backRefresh: false, //用于从上一级页面返回时刷新数据
				sheetStaic: false, //sheet显示状态
				//刷新图标
				refreshPic: "https://7463-tcb-jt4xhf2yawry64k-6chab0059a75-1316606514.tcb.qcloud.la/pic/refresh.png",
				//sheet图标
				sheetPic: "https://7463-tcb-jt4xhf2yawry64k-6chab0059a75-1316606514.tcb.qcloud.la/pic/pen.png",
				sheetList: [{
						name: '修改',
						fontSize: '16',
						type: "edit",
						disabled: true
					},
					{
						name: '删除',
						color: '#ff0004',
						subname: "仅管理员可执行删除操作，请谨慎执行",
						fontSize: '16',
						type: "del",
						disabled: true

					}
				], //修改和删除的sheet
				//optionResType:false, //记录当前题目回答是否正确
				opNow: "", //用于显示刚点击时，用户选了哪个选项
				answerType: false, //记录当前问题是否回答过，用于禁用选项按钮
				tabList: [], //用于保存tab中的索引值index所对应的name
				// article: marked('3^2^'),
				uid: "", //用户uid
				cid: 0, //判断现在是什么学科
				chapterId: 0, //判断第几章
				qidNum: 0, //当前题号
				questionList: [], //保存获取的题库数据
				optionBt: [ //用于显示选项按钮并且存储选项内容的数组
					{
						oid: 1, //1代表a 2b 3c 4d
						oName: "A", //选项A
						borderColor: "#3C9CFF", //记录按钮外边框颜色 默认：蓝色 对了绿色 错了红色 
						optionContent: "" //选项内容
					}, {
						oid: 2,
						oName: "B",
						borderColor: "#3C9CFF",
						optionContent: ""
					}, {
						oid: 3,
						oName: "C",
						borderColor: "#3C9CFF",
						optionContent: ""
					}, {
						oid: 4,
						oName: "D",
						borderColor: "#3C9CFF",
						optionContent: ""
					},
				],
				tagList: [ //用于显示tag内容
					{
						tagid: 1, //标记tagid
						tagContent: "408",
						tagColor: "#3C9CFF" //tag颜色
					},
					{
						tagid: 2, //标记tagid
						tagContent: "2018年第一题",
						tagColor: "#FF7043" //tag颜色
					},
					{
						tagid: 3, //标记tagid
						tagContent: "正确率:70%",
						tagColor: "#F56C6C" //tag颜色
					},

				],

			};
		},
		onShow() {
			var pages = getCurrentPages(); // 获取当前页面栈
			var currentPage = pages[pages.length - 1]; // 当前页面
			if (currentPage.data.backRefresh) {
				currentPage.data.backRefresh = false;
				if (this.mode == "nomal") {
					this.normalQuestion();

				} else if (this.mode == 0 || this.mode == 1) {
					let tem = {};
					tem.mode = this.mode;
					this.easyHard(tem);
				};
				uni.pageScrollTo({
					scrollTop: 0,
					duration: 0
				})
				// console.log("111RE");
			}
		},
		onUnload() {
			var pages = getCurrentPages(); //当前页面栈
			if (pages.length > 1) {
				var beforePage = pages[pages.length - 2]; //获取上一个页面实例对象
				beforePage.data.backRefresh = true;
				beforePage.data.cid = this.cid;
				beforePage.data.uid = this.uid;
			}
			//销毁考试记录
			if (this.mode == "exam"|| this.mode == "diyExam") {
				this.deleteExam();
				uni.reLaunch({
					url:"/pages/index/index"
				})
			}
		},
		onReady() {

		},
		onLoad(e) {
			console.log("e", e);
			this.chapterId = parseInt(e.chapterId);
			this.cid = parseInt(e.cid);
			this.uid = e.uid;
			this.getdsData(e);
			this.mode = e.mode;
		},
		methods: {
			//get获取题库
			async getdsData(e) {
				//点击历史记录时跳转
				if (e.qOrder) {
					this.qidNum = parseInt(e.qOrder - 1)
				}
				//防止恶意刷新
				const time = Date.now();
				if (time - this.getTime < 2000) {
					uni.showToast({
						title: "操作过于频繁，请稍后...",
						icon: "none",
						mask: true
					})
					return;
				}
				//全部题、基础、提高题目分流获取 0是基础 1是提高
				if (e.mode == 0 || e.mode == 1) {
					console.log("mode", e.mode);
					//提高篇、基础篇
					this.easyHard(e);

				} else if (e.mode == "se") {
					// console.log("seuid",this.uid);
					//顺序刷题
					this.seQuestion();

				} else if (e.mode == "random") {
					//随机刷题
					this.randomQuestion();

				} else if (e.mode == "nomal") {
					//按科目、章节刷题
					this.normalQuestion();

				} else if (e.mode == "exam") {
					this.examQuestion();

				} else if (e.mode == "diyExam") {
					this.diyExam(e);
				}
				
				

			},
			//按科目、章节刷题 正常模式
			async normalQuestion() {
				const questionData = questionFun.getdsData(this.uid, this.cid, this.chapterId);
				questionData.then(questionData => {
					this.questionList = questionData.data;
					this.saveOptionBt();
					this.nameMapOreder();
					this.saveTag();
					this.answerBool();
					this.skeleton = false;
					console.log("data",this.questionList);
				}).catch(() => {
					uni.showToast({
						title: "题目还未添加，正在尽快添加中！",
						icon: 'error'
					})
					setTimeout(() => {
						uni.navigateBack();
					}, 900)
				})
			},
			//提高篇、基础篇
			async easyHard(e) {
				const questionModeData = questionFun.getQuestionModeData(this.uid, this.cid, this.chapterId, e
					.mode);
				questionModeData.then(questionModeData => {
					// console.log("questionModeData",questionModeData);
					this.questionList = questionModeData.data;
					console.log("ql", this.questionList);
					//将数据库里的题号按现在模式的题号顺序排
					this.nameMapOreder();
					// console.log("q2", this.questionList);
					this.saveOptionBt();
					this.saveTag();
					this.answerBool();
					this.skeleton = false;
				}).catch(emm => {
					// console.log("emm",emm);
					uni.showToast({
						title: "题目还未添加，正在尽快添加中！",
						icon: 'error'
					})
					setTimeout(() => {
						uni.navigateBack();
					}, 900)
				})
			},
			//顺序刷题
			async seQuestion() {
				const sequestionData = questionFun.getSequestion(this.uid, this.cid);
				sequestionData.then(sequestionData => {
					// console.log("sequestionData",sequestionData);
					this.questionList = sequestionData.data;
					//将数据库里的题号按现在模式的题号顺序排,并将原本题目name保存到qOrder上，用于保存正确的历史记录中题目是第几题
					this.nameMapOreder();
					// console.log("se",this.questionList);
					this.saveOptionBt();
					this.saveTag();
					this.answerBool();
					this.skeleton = false;
				})
			},
			//随机刷题
			async randomQuestion() {
				this.questionList = [];
				// console.log("agin", this.questionList);
				const ChapteridNum = db.collection("chapter")
					.where({
						cid: this.cid
					}).count();

				ChapteridNum.then(res => {
					this.questionList = [];
					const cidNum = res.result.total - 1; //记录选择的学科有多少个章节
					const Chapterid = Math.round(Math.random() * cidNum); //记录随机学科有的章节id
					// console.log("ChapteridNum",Chapterid);
					this.chapterId = Chapterid;
					const randomData = questionFun.getRandomData(this.uid, this.cid, Chapterid);
					randomData.then(randomData => {
						this.questionList = randomData.data;
						//将数据库里的题号按现在模式的题号顺序排
						this.nameMapOreder();
						// console.log("se",this.questionList);
						this.saveOptionBt();
						this.saveTag();
						this.answerBool();
						this.randomRefresh = true;
						this.skeleton = false;

					})
				})
			},
			//统考组卷
			async examQuestion() {
				let DS = await this.getExamDsQuestion();
				let ZCYL = await this.getExamZCYLQuestion();
				let OS = await this.getExamOSQuestion();
				let WL = await this.getExamWLQuestion();
				this.nameMapOreder();
				this.saveOptionBt();
				this.saveTag();
				this.answerBool();
				this.examQueNum = this.questionList.length;
				this.skeleton = false;
			},
			//自主组卷
			async diyExam(e) {
				let examName = [];
				
				if (e.examName) {
					examName = e.examName.split(",");
				}
				for (let i = 0; i <= 3; i++) {
					if (examName[i] == "数据结构") {
						let DS = await this.getExamDsQuestion();
					} else if (examName[i] == "计算机组成原理") {
						let ZCYL = await this.getExamZCYLQuestion();
					} else if (examName[i] == "操作系统") {
						let OS = await this.getExamOSQuestion();
					} else if (examName[i] == "计算机网络") {
						let WL = await this.getExamWLQuestion();
					}
				}
				this.nameMapOreder();
				this.saveOptionBt();
				this.saveTag();
				this.answerBool();
				this.examQueNum = this.questionList.length;
				this.skeleton = false;


			},
			//获取数据结构考试题目
			async getExamDsQuestion() {
				let tempList = []; //用于暂时存放的题目数据，便于随机筛选考试题目
				const getDSExam = questionFun.getExamDsData(0, this.uid);
				let result = await getDSExam.then(res => {
					let questionNum = res.data.length - 1; //记录一共有多少题， -1是因为数组下标
					for (let i = 1; i < 11; i++) {
						let randomNum = Math.floor(Math.random() * questionNum);

						tempList = [...tempList, res.data[randomNum]];
						res.data.splice(randomNum, 1);
						questionNum = questionNum - 1;

					}
					this.questionList = [...this.questionList, ...tempList];
					console.log("que0", this.questionList);
					// this.getExamZCYLQuestion(); //获取组成原理考试题目
				})
			},
			//获取组成原理考试题目
			async getExamZCYLQuestion() {
				let tempList = []; //用于暂时存放的题目数据，便于随机筛选考试题目
				const getZCYLExam = questionFun.getExamDsData(3, this.uid);
				let result = await getZCYLExam.then(res => {
					let questionNum = res.data.length - 1; //记录一共有多少题， -1是因为数组下标
					for (let i = 1; i < 13; i++) {
						let randomNum = Math.floor(Math.random() * questionNum);
						tempList = [...tempList, res.data[randomNum]];
						res.data.splice(randomNum, 1);
						questionNum = questionNum - 1;
					}
					this.questionList = [...this.questionList, ...tempList];
					console.log("que1", this.questionList);
					// this.getExamOSQuestion();
				})
			},
			//获取操作系统原理考试题目
			async getExamOSQuestion() {
				let tempList = []; //用于暂时存放的题目数据，便于随机筛选考试题目
				const getOSExam = questionFun.getExamDsData(1, this.uid);
				let result = await getOSExam.then(res => {
					let questionNum = res.data.length - 1; //记录一共有多少题， -1是因为数组下标
					for (let i = 1; i < 11; i++) {
						let randomNum = Math.floor(Math.random() * questionNum);
						tempList = [...tempList, res.data[randomNum]];
						res.data.splice(randomNum, 1);
						questionNum = questionNum - 1;
					}
					this.questionList = [...this.questionList, ...tempList];
					console.log("que2", this.questionList);
					// this.getExamWLQuestion();
				})
			},
			//获取计算机网络考试题目
			async getExamWLQuestion() {
				let tempList = []; //用于暂时存放的题目数据，便于随机筛选考试题目
				const getWLExam = questionFun.getExamDsData(2, this.uid);
				let result = await getWLExam.then(res => {
					let questionNum = res.data.length - 1; //记录一共有多少题， -1是因为数组下标
					for (let i = 1; i < 9; i++) {
						let randomNum = Math.floor(Math.random() * questionNum);
						tempList = [...tempList, res.data[randomNum]];
						res.data.splice(randomNum, 1);
						questionNum = questionNum - 1;
					}
					this.questionList = [...this.questionList, ...tempList];
					console.log("que3", this.questionList);
				})
			},
			//销毁考试记录
			deleteExam() {
				db.collection("exam").where({
					uid: this.uid
				}).remove();
			},

			//将数据库里的题号按现在模式的题号顺序排,并将原本题目name保存到qOrder上，用于保存正确的历史记录中题目是第几题
			nameMapOreder() {
				for (let i = 0; i < this.questionList.length; i++) {
					let qOrder = this.questionList[i].name;
					this.questionList[i].qOrder = qOrder;
					this.questionList[i].name = i + 1;
				}
			},

			//将当前问题的选择内容处理保存到optionBt
			saveOptionBt() {
				this.optionBt[0].optionContent = this.questionList[this.qidNum]
					.AoptionContent;
				this.optionBt[1].optionContent = this.questionList[this.qidNum]
					.BoptionContent;
				this.optionBt[2].optionContent = this.questionList[this.qidNum]
					.CoptionContent;
				this.optionBt[3].optionContent = this.questionList[this.qidNum]
					.DoptionContent;
				console.log("this.optionBt", this.optionBt);
			},
			//处理当前问题tag
			saveTag() {
				this.tagList[0].tagContent = this.questionList[this.qidNum].tagSource;
				this.tagList[1].tagContent = this.questionList[this.qidNum].tagSection;
				if (this.questionList[this.qidNum].tagDifficulty == 0) {
					this.tagList[2].tagContent = "基础篇";
				} else if (this.questionList[this.qidNum].tagDifficulty == 1) {
					this.tagList[2].tagContent = "提高篇";
				}

			},

			// 点击tab改变题号
			clicktab(item) {
				console.log('item', item);
				this.qidNum = item.index;
				this.clearOptionBt();
				this.saveOptionBt();
				this.saveTag();
				this.answerBool();
			},

			// 点击切换题号
			nextClick(even) { //even 0是点击上一题 1是点击下一题
				if (even == 0) {
					this.qidNum = this.qidNum - 1;
					this.clearOptionBt();
					this.saveOptionBt();
					this.saveTag();
					this.answerBool();

				} else if (even == 1 || even < this.questionList.length - 1) {
					this.qidNum = this.qidNum + 1;
					this.clearOptionBt();
					this.saveOptionBt();
					this.saveTag();
					this.answerBool();
				}
			},
			//清楚上一条数据遗留数据
			clearOptionBt() {
				this.answerType = false;
				this.collectType = false;
				this.opNow = "";
				for (let i = 0; i < 4; i++) {
					this.optionBt[i].borderColor = "#3C9CFF";
					this.optionBt[i].optionContent = "";
				}
				for (let i = 0; i < 3; i++) {
					this.tagList[i].tagContent = "";
				}
			},

			//显示历史回答
			showHistoryAsw() {
				console.log("showHistoryAsw");
				//将考试和历史记录分开
				if (this.mode == "exam"||this.mode == "diyExam") {
					if (this.questionList[this.qidNum]._id.exam[0].optionResType == true) {
						for (let i = 0; i < 4; i++) {
							if (this.questionList[this.qidNum].answer == this.optionBt[i]
								.oName) {
								this.optionBt[i].borderColor = "#2BDE73";
								this.opNow = this.optionBt[i].oName;
								console.log("历史回答true", this.optionBt[i].oName);
							}
						}
					} else if (this.questionList[this.qidNum]._id.exam[0].optionResType ==
						false) {
						for (let i = 0; i < 4; i++) {
							if (this.questionList[this.qidNum].answer == this.optionBt[i]
								.oName) {
								this.optionBt[i].borderColor = "#2BDE73";
								console.log("历史回答cuodui", this.optionBt[i].oName);
							}
							if (this.questionList[this.qidNum]._id.exam[0].optionRes ==
								this.optionBt[i].oName) {
								this.optionBt[i].borderColor = "#FF3B1D";
								this.opNow = this.optionBt[i].oName;
								console.log("历史回答cuocuo", this.optionBt[i].oName);
							}
						}

					}
				} else {
					if (this.questionList[this.qidNum]._id.history_person[0]
						.optionResType == true) {
						for (let i = 0; i < 4; i++) {
							if (this.questionList[this.qidNum].answer == this.optionBt[i]
								.oName) {
								this.optionBt[i].borderColor = "#2BDE73";
								this.opNow = this.optionBt[i].oName;
								console.log("历史回答true", this.optionBt[i].oName);
							}
						}
					} else if (this.questionList[this.qidNum]._id.history_person[0]
						.optionResType == false) {
						for (let i = 0; i < 4; i++) {
							if (this.questionList[this.qidNum].answer == this.optionBt[i]
								.oName) {
								this.optionBt[i].borderColor = "#2BDE73";
								console.log("历史回答cuodui", this.optionBt[i].oName);
							}
							if (this.questionList[this.qidNum]._id.history_person[0]
								.optionRes == this.optionBt[i].oName) {
								this.optionBt[i].borderColor = "#FF3B1D";
								this.opNow = this.optionBt[i].oName;
								console.log("历史回答cuocuo", this.optionBt[i].oName);
							}
						}

					}
				}


			},

			//获取用户点击选项并更添加到历史记录表中
			async getOption(oid, oName) {
				this.opNow = oName;
				const qid = this.questionList[this.qidNum]._id._value;
				const qOrder = this.questionList[this.qidNum].qOrder;
				console.log("qOrder", this.questionList[this.qidNum].qOrder);
				if (this.answerType == false) {
					//为0，意味着用户还没有做个该题，获取选项后更新数据库
					const optionResType = this.rightOrWrong(qid, oid, oName);
					//顺序刷题是因为章节信息无法从上页面传参获得，这里改为用题目数组自身所带chapterId去改变this.chapterId
					this.chapterId = this.questionList[this.qidNum].Chapterid;
					if (this.mode == "exam" ||this.mode == "diyExam") {
						const subjectNameExam = await this.getSubjectName(this
							.questionList[this.qidNum].Cid);
						const chapterNameExam = await this.getChapterName(this
							.questionList[this.qidNum].Cid, this
							.questionList[this.qidNum].Chapterid);
						const addUserEXOptionRes = await questionFun
							.updataExamUserOptionRes(
								this.uid, this.cid, subjectNameExam, this.chapterId,
								chapterNameExam, qid, qOrder,
								oName,
								optionResType
							);
					} else {
						const subjectName = await this.getSubjectName(this.cid);
						const chapterName = await this.getChapterName(this.cid, this
							.chapterId);
						const addUserOptionRes = await questionFun.updataUserOptionRes(
							this.uid, this.cid, subjectName, this.chapterId,
							chapterName, qid, qOrder, oName,
							optionResType
						);
					}
					// if (this.mode == "exam") {

					// } else {


					// }
					await this.decideBrushes(this.uid);
				} else if (this.answerType == true) {
					//为1，代表用户这题做过了，要显示用户选过的选项
				} else {
					uni.showToast({
						icon: 'error',
						title: "获取历史信息错误"
					})
				}
			},
			//获取该题对应的学科名称
			async getSubjectName(cid) {
				const getsubjectName = await db.collection(
						"subject_classification")
					.where({
						cid: cid
					}).field("subjectName")
					.get();
				const subjectName = await getsubjectName.result.data[0]
					.subjectName;
				return subjectName;
			},
			//获取该题对应的章节名称
			async getChapterName(cid, chapterid) {
				const getchapterName = await db.collection("chapter")
					.where({
						cid: cid,
						Chapterid: chapterid
					}).field("ChapterName")
					.get();
				const chapterName = await getchapterName.result.data[0]
					.ChapterName;
				return chapterName;
			},
			//判断是否收藏 要是获取的数据没有collectType就添加到本地数组中
			// async collectBool(){
			// 	console.log("qidNum",this.qidNum);
			// 	console.log("qidLit",this.questionList[this.qidNum]);
			// 	if(!this.questionList[this.qidNum]._id.history_person[0]?.collectType){
			// 		this.collectType = false;
			// 		console.log("gb",this.questionList[this.qidNum]);
			// 	}else if(this.questionList[this.qidNum]._id.history_person[0].collectType == true){
			// 		this.collectType = true;
			// 	}else if(this.questionList[this.qidNum]._id.history_person[0].collectType == false){
			// 		this.collectType = false;
			// 	}
			// },
			
			
			//判断当前题目是否回答过
			answerBool() {
				//顺便是否收藏也判断下
				// this.collectBool();
				//将历史记录和考试记录分开来对比
				if (this.mode == "exam" ||this.mode == "diyExam") {
					if (this.questionList[this.qidNum]._id.exam.length == 0) {
						this.answerType = false;
						// console.log("没回答过this.qidNum", this.qidNum);
						// console.log("没回答过", this.questionList[this.qidNum]._id.history_person.length);
					} else if (this.questionList[this.qidNum]._id.exam
						.length == 1) {
						this.answerType = true;
						this.showHistoryAsw();
						// console.log("回答过");
					} else if (this.questionList[this.qidNum]._id.exam.length >
						1) {
						// console.log("获取判断当前题目是否回答过失败,非法历史记录");
					}

				} else {
					if (this.questionList[this.qidNum]._id.history_person
						.length == 0) {
						this.answerType = false;
						// console.log("没回答过this.qidNum", this.qidNum);
						// console.log("没回答过", this.questionList[this.qidNum]._id.history_person.length);
					} else if (this.questionList[this.qidNum]._id
						.history_person.length == 1) {
						this.answerType = true;
						this.showHistoryAsw();
						// console.log("回答过");
					} else if (this.questionList[this.qidNum]._id
						.history_person.length > 1) {
						// console.log("获取判断当前题目是否回答过失败,非法历史记录");
					}
				}


			},

			//判断答案正误
			rightOrWrong(qid, oid, oName) {
				if (this.questionList[this.qidNum].answer == oName) {
					//将组卷和历史记录分开
					if (this.mode == "exam"||this.mode == "diyExam") {
						this.questionList[this.qidNum]._id.exam.push({
							optionResType: true,
							optionRes: `${oName}`
						});
						this.resOPS = this.resOPS + 1;
						this.examOpsRight = this.examOpsRight + 2;
					} else {
						this.questionList[this.qidNum]._id.history_person
							.push({
								optionResType: true,
								optionRes: `${oName}`
							});
					}

					// console.log("正确本地记录", this.questionList[this.qidNum]._id);
					this.optionBt[oid - 1].borderColor = "#2BDE73";
					this.answerType = true;
					questionFun.updataUserRight(this.uid)
					return true;
				} else {
					//将组卷和历史记录分开
					if (this.mode == "exam"||this.mode == "diyExam") {
						this.questionList[this.qidNum]._id.exam.push({
							optionResType: false,
							optionRes: `${oName}`
						});
						this.resOPS = this.resOPS + 1;
					} else {
						this.questionList[this.qidNum]._id.history_person
							.push({
								optionResType: false,
								optionRes: `${oName}`
							});
					}

					// console.log("错误本地记录",this.questionList[this.qidNum]._id);
					this.optionBt[oid - 1].borderColor = "#FF3B1D";
					this.answerType = true;
					for (let i = 0; i < 4; i++) {
						if (this.questionList[this.qidNum].answer == this
							.optionBt[i].oName) {
							this.optionBt[i].borderColor = "#2BDE73";
						}

					}
					return false;
				}

			},
			//判断是要跟新还是新增刷题数
			decideBrushes(uid) {
				let brushesLen = 2;
				db.collection("userBrushes").where({
						uid: uid
					}).count()
					.then(res => {
						console.log("res.result.total", res.result.total);
						brushesLen = res.result.total;
						console.log("brushesLen", brushesLen);
						this.updataUserBrushes(uid, brushesLen);
					});
			},
			//更新和新增刷题数
			async updataUserBrushes(uid, brushesLen) {
				console.log("len", brushesLen);
				console.log("uid", uid);
				if (brushesLen > 0) {
					console.log("true");
					const updataUserBrushes = questionFun
						.updataUserBrushes(uid);
					console.log("updataUserBrushes", updataUserBrushes);
				} else if (brushesLen == 0) {
					console.log("flase");
					db.collection("userBrushes")
						.add({
							uid: uid,
							brushProgress: 1,
							accuracyNum: 1
						}).then(res => {
							console.log("add", res);
						}).catch(() => {
							console.log("add失败", );
						})
				}
			},
			//切换sheet状态并校验权限
			swtichSheetStaic() {
				this.sheetStaic = true;
				if (this.uniIDHasRole('wxadmin')) {
					for (let i = 0; i < 2; i++) {
						this.sheetList[i].disabled = false;
					}
				} else if (this.uniIDHasRole('entryClerk')) {
					this.sheetList[0].disabled = false;
				}
				// console.log('当前用户是否拥有管理员角色：', this.uniIDHasRole('wxadmin'))
				// console.log('当前用户是否拥有entryClerk角色：', this.uniIDHasRole('entryClerk'))

			},

			//关闭sheet
			closeSheet() {
				this.sheetStaic = false;
			},
			//sheet点击选择并跳转问题修改或删除
			selectSheet(e) {
				if (e.type == "edit") {
					uni.navigateTo({
						url: "/pages/addQuestion/addQuestion?mode=" +
							"edit" + "&cid=" + this.cid +
							"&chapterId=" +
							this.chapterId + "&qid=" + this
							.questionList[this.qidNum]._id._value +
							"&uid=" + this
							.uid
					})
				} else if (e.type == "del") {
					console.log("que", this.questionList[this.qidNum]);
					uni.showModal({
						title: "删除提醒",
						content: "注意该为删除操作！",
						success: (success => {
							if (success.confirm) {
								db.collection("ds").where({
										_id: this
											.questionList[
												this
												.qidNum
											]._id
											._value
									}).remove()
									.then(res => {
										uni.showToast({
											title: "删除成功",
											icon: 'none'
										})
										setTimeout(
											() => {
												uni
													.navigateBack();
											}, 900)


									})
							} else if (success.cancel) {
								// console.log('用户点击取消');
							}
						})
					})
				}

			},
			//点击收藏
			myCollect() {
				// this.collectType = !this.collectType;
				// console.log("1");
				// if(this.collectType == true){
				// 	const collectTypeTrue = questionFun.updataCollectData(this.uid,this.questionList[this.qidNum].Cid,this.questionList[this.qidNum].Chapterid,this.questionList[this.qidNum]._id._value,true);
				// 	collectTypeTrue.then(res=>{
				// 		uni.showToast({
				// 			icon: 'none',
				// 			title: "题目收藏成功"
				// 		})
				// 		this.questionList[this.qidNum]._id.history_person[0].collectType=true;
				// 	}).catch(()=>{
				// 		uni.showToast({
				// 			icon: 'error',
				// 			title: "题目收藏失败"
				// 		})
				// 	})
					
				// }
				// else{
				// 	const collectTypeFalse = questionFun.updataCollectData(this.uid,this.questionList[this.qidNum].Cid,this.questionList[this.qidNum].Chapterid,this.questionList[this.qidNum]._id._value,false);
				// 	collectTypeFalse.then(res=>{
				// 		uni.showToast({
				// 			icon: 'none',
				// 			title: "取消题目收藏"
				// 		})
				// 		this.questionList[this.qidNum]._id.history_person[0].collectType=false;
				// 	}).catch(()=>{
				// 		uni.showToast({
				// 			icon: 'error',
				// 			title: "取消题目收藏失败"
				// 		})
				// 	})
				// }
				uni.showToast({
					icon: 'none',
					title: "开发中，很快实装拉"
				})
			},
			//随机刷题，刷新图标
			clickRefresh() {
				this.questionList = [];
				this.clearOptionBt();
				this.qidNum = 0;
				this.skeleton = true;
				//节流防抖
				if (this.oldTime == '') {

					this.randomQuestion();
					this.oldTime = new Date().getTime();

				} else {
					let newTime = new Date().getTime();
					if (newTime - this.oldTime > 2500) {
						this.oldTime = new Date().getTime();
						this.randomQuestion();
					} else {
						this.skeleton = true;
						uni.showToast({
							icon: 'none',
							title: "操作过快，请稍后在试"
						})

					}
				}

			}


			//获取问题区域高度
			// getQuestionBoxHeigh(){
			// 	const query = uni.createSelectorQuery().in(this);
			// 	query.select('.optionButtonBox').boundingClientRect(data => {
			// 	  console.log("得到布局位置信息" + JSON.stringify(data));
			// 	  console.log("节点离页面顶部的距离为" + data.height);
			// 	}).exec();
			// 	// console.log("questionBox",questionBox);
			// 	// const query = uni.createSelectorQuery().in(this)
			// 	//                 query
			// 	//                     .select(".questionBox")
			// 	//                     .boundingClientRect(data => {
			// 	//                         console.log(data.height)
			// 	//                     })
			// 	//                     .exec();
			// }
		}

	}
</script>

<style lang="scss">
	page {
		background-color: #f8f8f8;
		position: relative;
		height: 100%;
	}

	.tabBox {
		background-color: #fff;
	}

	.buttonRow {
		background-color: #fff;
		display: flex;
		align-items: center;
		justify-content: space-evenly;
		padding: 20rpx 12rpx 50rpx 12rpx;
		position: absolute;
		bottom: 0;
		width: 100%;

		.collect {
			width: 25%;
		}

		.nextButton {
			width: 30%;
		}
	}

	.questionBox {
		overflow-y: scroll;
		height: calc(100vh - 230rpx);
		display: flex;
		flex-direction: column;

		.questionTitle {
			padding: 20rpx 12rpx;
			margin: 12rpx;
			background-color: #fff;
			border-radius: 16rpx;
			border: 1rpx solid #3C9CFF;
			font-size: 28rpx;
			color: #333;
			font-weight: bold;
		}

		.tagBox {
			display: flex;
			align-items: center;
			padding-left: 12rpx;
			flex-wrap: wrap;

			.tagItem {
				margin: 8rpx;
				font-size: 22rpx;
				color: white;
				padding: 12rpx;
				border-radius: 16rpx;
			}
		}

		.questionOptionRow {
			display: flex;
			align-items: center;
			padding: 18rpx 28rpx;

			.optionName {
				color: #333;
				font-weight: bold;
				font-size: 28rpx;
			}

			.optionContent {
				color: #333;
				font-size: 28rpx;
			}
		}

		.optionButtonBox {
			display: flex;
			flex-wrap: wrap;
			justify-content: space-evenly;
			align-items: center;
			padding: 100rpx 20rpx 15rpx 20rpx;

			.optionButtonItem {
				background-color: #fff;
				width: 40%;
				// border: 1rpx solid #3C9CFF;
				text-align: center;
				padding: 18rpx 0;
				margin: 12rpx 0;
				border-radius: 16rpx;


			}
		}

		.analysisBox {
			background-color: #F5E0A3;
			margin: 12rpx;
			padding: 15rpx 12rpx;
			border-radius: 16rpx;
			border: 2rpx solid #3C9CFF;

			.analysisRight {
				color: #F6003C;
				padding: 15rpx 12rpx;
			}

			.userOption {
				color: #333;
				padding: 15rpx 12rpx;
			}

			.analysisContent {
				padding: 15rpx 12rpx;
				line-height: 1.5em;
			}
		}


	}

	.modifyBox {
		width: 52rpx;
		height: 52rpx;
		position: absolute;
		right: 2rpx;
		bottom: 28%;
		background-color: #ffffff;
		border-radius: 50%;
		background-size: 100% 100%;
		background-repeat: no-repeat;
		border: 5rpx solid #333;
		padding: 20rpx;
	}

	.refresh {
		width: 52rpx;
		height: 52rpx;
		position: absolute;
		right: 2rpx;
		bottom: 17%;
		// background-color: #ffffff;
		border-radius: 50%;
		background-size: 100% 100%;
		background-repeat: no-repeat;
		// border: 5rpx solid #333;
		padding: 20rpx;
	}

	.examBox {
		display: flex;
		justify-content: space-evenly;

		.resItem {
			width: 50%;
			color: #FF585D;
			font-size: 28rpx;
			font-weight: bold;
			text-align: center;
		}

		// .timeCont{
		// 	display: flex;
		// 	width: 40%;
		// 	font-size: 28rpx;
		// 	color: #888;
		// 	.time{
		// 		padding-left: 4rpx;
		// 	}
		// }
	}

	.border {
		position: relative;
	}

	.border::after {
		border-bottom: 1px solid #eee;
		bottom: 0;
		box-sizing: border-box;
		content: " ";
		left: 12rpx;
		pointer-events: none;
		position: absolute;
		right: 12rpx;
		transform: scaleY(.5);
		transform-origin: center;
	}
</style>